﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Namespace Active.Modules.Forums.Controls
    Public Class ControlsBase
        Inherits WebControl
#Region "Constants"
        Public Const ControlTag As String = "<%@ Register TagPrefix=""af"" Assembly=""Active.Modules.Forums"" Namespace=""Active.Modules.Forums.Controls""%>"
#End Region
#Region "Enum"
        Public Enum ViewContextTypes
            ForumList
            ForumGroup
            ForumTopics
            Thread
            Topic
            Reply
            Editor
            Profile
            Other
        End Enum
#End Region
#Region "Private Members"
        Private _template As String
        Private _data As XmlDocument
        Private _topicData As XmlDocument
        Private _topicsData As XmlDocument
        Private _templateFile As String
        Private _controlConfig As ControlsConfig
        Private _forumId As Integer = -1
        Private _topicId As Integer
        Private _currentView As String = "forumview"
        Private _pageId As Integer
        Private _parseTemplate As Boolean = False

#End Region
#Region "Public Properties"
        Public Property ParseTemplateFile() As Boolean
            Get
                Return _parseTemplate
            End Get
            Set(ByVal value As Boolean)
                _parseTemplate = value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
         Public Property DisplayTemplate() As String
            Get
                If String.IsNullOrEmpty(_template) And Not String.IsNullOrEmpty(_templateFile) Then
                    If Not String.IsNullOrEmpty(ControlConfig.TemplatePath) Then
                        _template = ControlConfig.TemplatePath & _templateFile
                    Else
                        _template = _templateFile
                    End If
                    _template = Utilities.GetTemplate(Page.ResolveUrl(_template))
                    _template = Utilities.ParseTokenConfig(_template, "default", ControlConfig)
                End If
                Return _template
            End Get
            Set(ByVal Value As String)
                _template = Value
            End Set
        End Property

        Public Property Data() As XmlDocument
            Get
                Return _data
            End Get
            Set(ByVal value As XmlDocument)
                _data = value
            End Set
        End Property
        Public Property TopicsData() As XmlDocument
            Get
                Return _topicsData
            End Get
            Set(ByVal value As XmlDocument)
                _topicsData = value
            End Set
        End Property
        Public Property TopicData() As XmlDocument
            Get
                Return _topicData
            End Get
            Set(ByVal value As XmlDocument)
                _topicData = value
            End Set
        End Property
        Public Property TemplateFile() As String
            Get
                Return _templateFile
            End Get
            Set(ByVal value As String)
                _templateFile = value
            End Set
        End Property
        Public Property ControlConfig() As ControlsConfig
            Get
                Return _controlConfig
            End Get
            Set(ByVal value As ControlsConfig)
                _controlConfig = value
            End Set
        End Property
        Public Property ForumId() As Integer
            Get
                If Not HttpContext.Current.Request.QueryString(ParamKeys.ForumId) Is Nothing Then
                    Return Integer.Parse(HttpContext.Current.Request.QueryString(ParamKeys.ForumId).ToString)
                Else
                    Return _forumId
                End If
            End Get
            Set(ByVal value As Integer)
                _forumId = value
            End Set
        End Property

        Public Property TopicId() As Integer
            Get
                If Not HttpContext.Current.Request.QueryString(ParamKeys.TopicId) Is Nothing Then
                    Return Integer.Parse(HttpContext.Current.Request.QueryString(ParamKeys.TopicId).ToString)
                Else
                    Return _topicId
                End If
            End Get
            Set(ByVal value As Integer)
                _topicId = value
            End Set
        End Property
        Public Property CurrentView() As String
            Get
                Return _currentView
            End Get
            Set(ByVal value As String)
                _currentView = value
            End Set
        End Property

#End Region
#Region "Read Only Properties"
        Public ReadOnly Property ViewContext() As ViewContextTypes
            Get
                If CurrentView = Views.ForumView.ToLowerInvariant And HttpContext.Current.Request.QueryString(ParamKeys.GroupId) Is Nothing Then
                    Return ViewContextTypes.ForumList
                ElseIf CurrentView = Views.ForumView.ToLowerInvariant And Not HttpContext.Current.Request.QueryString(ParamKeys.GroupId) Is Nothing Then
                    Return ViewContextTypes.ForumGroup
                ElseIf CurrentView = Views.Topics.ToLowerInvariant Then
                    Return ViewContextTypes.ForumTopics
                ElseIf CurrentView = Views.Topic.ToLowerInvariant Then
                    Return ViewContextTypes.Thread
                ElseIf CurrentView = Views.TopicEdit.ToLowerInvariant Or CurrentView = Views.TopicNew.ToLowerInvariant Then
                    Return ViewContextTypes.Editor
                End If

            End Get
        End Property
        Public ReadOnly Property InstanceId() As Integer
            Get
                Return ControlConfig.InstanceId
            End Get
        End Property
        Public ReadOnly Property SiteId() As Integer
            Get
                Return ControlConfig.SiteId
            End Get
        End Property
        Public ReadOnly Property PageId() As Integer
            Get
                Return ControlConfig.PageId
            End Get
        End Property
        Public ReadOnly Property UserId() As Integer
            Get
                Return ControlConfig.User.UserID
            End Get
        End Property
        Public ReadOnly Property UserRoles() As String
            Get
                Return ControlConfig.User.UserRoles
            End Get
        End Property
        Public ReadOnly Property ThemePath() As String
            Get
                Return ControlConfig.ThemePath
            End Get
        End Property
        Public ReadOnly Property DataPageId() As Integer
            Get
                If HttpContext.Current.Request.QueryString(ParamKeys.PageId) Is Nothing Then
                    Return 1
                Else
                    Return Integer.Parse(HttpContext.Current.Request.QueryString(ParamKeys.PageId).ToString)
                End If
            End Get
        End Property
        Public ReadOnly Property ForumName() As String
            Get
                If Data Is Nothing Then
                    Return String.Empty
                Else
                    Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & ForumId & "']")
                    If Not xNode Is Nothing Then
                        Return xNode.Attributes("name").Value.ToString
                    End If
                End If
                Return String.Empty
            End Get
        End Property
        Public ReadOnly Property ForumName(ByVal fid As Integer) As String
            Get
                If Data Is Nothing Then
                    Return String.Empty
                Else
                    Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & fid & "']")
                    If Not xNode Is Nothing Then
                        Return xNode.Attributes("name").Value.ToString
                    End If
                End If
                Return String.Empty
            End Get
        End Property

        Public ReadOnly Property GroupName() As String
            Get
                If Data Is Nothing Then
                    Return String.Empty
                Else
                    Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & ForumId & "']")
                    If Not xNode Is Nothing Then
                        Dim groupId As Integer = Integer.Parse(xNode.Attributes("groupid").Value.ToString)
                        xNode = Data.SelectSingleNode("//groups/group[@groupid='" & groupId & "']")
                        If Not xNode Is Nothing Then
                            Return xNode.Attributes("name").Value.ToString
                        End If
                    End If
                End If
                Return String.Empty
            End Get
        End Property
        Public ReadOnly Property GroupName(ByVal fid As Integer) As String
            Get
                If Data Is Nothing Then
                    Return String.Empty
                Else
                    Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & fid & "']")
                    If Not xNode Is Nothing Then
                        Dim groupId As Integer = Integer.Parse(xNode.Attributes("groupid").Value.ToString)
                        xNode = Data.SelectSingleNode("//groups/group[@groupid='" & groupId & "']")
                        If Not xNode Is Nothing Then
                            Return xNode.Attributes("name").Value.ToString
                        End If
                    End If
                End If
                Return String.Empty
            End Get
        End Property
        Public ReadOnly Property CurrentForum() As Forum
            Get
                Return CurrentForum(ForumId)
            End Get
        End Property
        Public ReadOnly Property CurrentForum(ByVal fid As Integer) As Forum
            Get
                If Data Is Nothing Then
                    Return Nothing
                Else
                    Dim fi As Forum = Nothing
                    Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & fid & "']")
                    If Not xNode Is Nothing Then
                        fi = New Forum
                        With fi
                            .ForumName = xNode.Attributes("name").Value.ToString
                            .AllowRSS = Boolean.Parse(xNode.Attributes("allowrss").Value.ToString)
                            '.Permissions = Permissions.GetPermissionSet(xNode.SelectSingleNode("//permissions").OuterXml)
                        End With
                        Return fi
                    End If
                End If
                Return Nothing
            End Get
        End Property
#End Region
#Region "Event Handlers"
        Private Sub ControlsBase_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If ParseTemplateFile Then
                Dim ctl As Control = Page.ParseControl(DisplayTemplate)
                LinkControls(ctl.Controls)
                Me.Controls.Add(ctl)


            End If
        End Sub
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
            Dim htmlWriter As Web.UI.HtmlTextWriter = New Web.UI.HtmlTextWriter(stringWriter)
            MyBase.Render(htmlWriter)
            Dim html As String = stringWriter.ToString()
            html = Utilities.ParseToolBar(html, PageId, InstanceId, Me.UserId, Me.ControlConfig.User.CurrentUserType)
            html = Utilities.LocalizeControl(html)
            html = html.Replace("[EMOTICONPATH]", Page.ResolveUrl("~/DesktopModules/ActiveForums/Themes/images/emoticons/"))
            writer.Write(html)
        End Sub
#End Region
#Region "Private Methods"
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                If TypeOf ctrl Is Controls.ForumRow Then
                    DirectCast(ctrl, Controls.ForumRow).UserRoles = UserRoles
                End If
                If TypeOf ctrl Is Controls.ControlsBase Then
                    DirectCast(ctrl, Controls.ControlsBase).ControlConfig = Me.ControlConfig
                    DirectCast(ctrl, Controls.ControlsBase).Data = Me.Data
                    DirectCast(ctrl, Controls.ControlsBase).TopicsData = Me.TopicsData
                    DirectCast(ctrl, Controls.ControlsBase).TopicData = Me.TopicData
                End If
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
#End Region
#Region "Friend Methods"
        Friend Function ParseForumData(ByVal template As String) As String
            If Not Data Is Nothing Then
                Dim xNode As System.Xml.XmlNode = Data.SelectSingleNode("//forums/forum[@forumid='" & ForumId & "']")
                If Not xNode Is Nothing Then
                    Dim forumname As String = xNode.Attributes("name").Value.ToString
                    Dim groupId As Integer = Integer.Parse(xNode.Attributes("groupid").Value.ToString)
                    template = template.Replace("[FORUMNAME]", forumname)
                    template = template.Replace("[FORUMID]", ForumId.ToString)
                    template = template.Replace("[GROUPID]", groupId.ToString)
                    template = template.Replace("[FORUMGROUPID]", groupId.ToString)

                    xNode = Data.SelectSingleNode("//groups/group[@groupid='" & groupId & "']")
                    If Not xNode Is Nothing Then
                        template = template.Replace("[GROUPNAME]", xNode.Attributes("name").Value.ToString)
                    End If

                End If

            End If
            Return template
        End Function
#End Region

    End Class
End Namespace

